<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <el-card v-if="subjectIds">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item class="sub" :to="{ path: '/subjects/list' }">
          学科管理</el-breadcrumb-item
        >
        <el-breadcrumb-item>{{ subNames }}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-card>

    <el-card>
      <!-- 搜索 -->
      <el-row type="flex">
        <el-col :span="6">
          <span class="zz">目录名称</span>
          <el-input
            style="width: 240px"
            v-model="form.directoryName"
          ></el-input>
        </el-col>
        <el-col :span="10">
          <span class="zz">状态</span>
          <el-select v-model="form.state">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <el-button class="btn" @click="clear">清除</el-button>
          <el-button type="primary" @click="search(form)">搜索</el-button>
        </el-col>
        <el-col :span="8">
          <el-row type="flex" justify="end">
            <el-button
              type="text"
              v-if="subjectIds"
              @click="$router.push('/subjects/list')"
            >
              <i class="el-icon-back"></i>返回学科
            </el-button>
            <el-button type="success" @click="addDirectorys()"
              >新增目录</el-button
            >
          </el-row>
        </el-col>
      </el-row>

      <el-alert
        :title="`数据一共${this.page.total}条`"
        type="info"
        class="alert"
        :closable="true"
        show-icon
      ></el-alert>

      <!-- 表格 -->
      <el-table :data="DirectoryList" style="width: 100%">
        <el-table-column label="序号" type="index" width="80px">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template v-slot="row">
            {{ row.row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="row">
            {{ row.row.state == 0 ? "已禁用" : "已启用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="row">
            <el-button type="text" @click="changeStatus(row)">
              {{ row.row.state !== 0 ? "禁用" : "启用" }}
            </el-button>
            <el-button
              type="text"
              :disabled="isDisabled(row)"
              @click="edit(row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="isDisabled(row)"
              @click="remove(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          layout="sizes,prev,pager,next,jumper"
          :total="page.total"
          :current-page="page.page"
          :page-size="page.pagesize"
          :page-sizes="page.pagesizes"
          @current-change="PageChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 新增目录弹出层 -->
    <directorysAdd
      :showDirectorys="showDirectorys"
      :subjectIds="subjectIds"
      ref="DirectorysRef"
      @closeDirectorys="closeDirectorys((showDirectorys = false))"
    ></directorysAdd>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/directorys.js'
import directorysAdd from '../components/directorys-add.vue'
export default {
  components: {
    directorysAdd
  },
  data () {
    return {
      DirectoryList: [],
      subjectIds: '', // 路由上的学科ID
      options: [{
        value: '0',
        label: '已禁用'
      },
      {
        value: '1',
        label: '已启用'
      }],
      page: {
        page: 1, // 当前页码
        pagesize: 10,
        total: 0, // 总数
        pagesizes: [10, 20],
        pages: 0
      },
      form: {
        directoryName: '',
        state: ''
      },
      directoryForm: {
        id: '',
        subjectID: '',
        directoryName: ''
      },
      showDirectorys: false, // 控制弹出层的显示隐藏
      subNames: ''
    }
  },
  created () {
    this.subjectIds = this.$route.query.id
    this.getDirectoryList()
  },
  methods: {
    // 获取目录列表
    async getDirectoryList () {
      if (this.subjectIds) {
        const { data } = await list({ subjectID: this.subjectIds })
        this.DirectoryList = data.items
        this.page.page = +data.page
        this.page.pages = +data.pages
        if (data.items.length === 0) {
          this.subNames = '暂无数据'
        } else {
          this.subNames = data.items[0].subjectName
        }
        this.page.total = data.counts
      } else {
        const { data } = await list(this.page)
        this.DirectoryList = data.items
        this.page.total = data.counts
        this.page.page = +data.page
        this.page.pages = +data.pages
      }
      if (this.page.page > this.page.pages) {
        this.PageChange(+this.page.pages)
      }
    },
    // 新增目录
    addDirectorys () {
      this.showDirectorys = true
      this.$refs.DirectorysRef.form.id = null
    },
    closeDirectorys () {
      this.showDirectorys = false
    },
    PageChange (newPage) {
      this.page.page = newPage
      this.getDirectoryList()
    },
    // 页码选择
    handleSizeChange (newPage) {
      this.page.pagesize = newPage
      this.getDirectoryList()
    },
    // 清除
    clear () {
      this.form.directoryName = ''
      this.form.state = ''
      this.getDirectoryList()
    },
    // 搜索
    async  search (form) {
      if (this.form.directoryName.trim() === '' && this.form.state === '') {
        return this.$message.error('数据不能为空')
      }
      const req = {}
      Object.keys(form).filter(i => form[i]).forEach(i => { req[i] = form[i] })
      const { data } = await list(req)
      this.DirectoryList = data.items
      this.page.total = this.DirectoryList.length
    },
    isDisabled (row) {
      if (row.row.state === 0) {
        return false
      } else {
        return true
      }
    },
    // 改变状态
    async changeStatus (row) {
      await changeState({
        id: row.row.id,
        state: row.row.state === 0 ? '1' : '0'
      })
      this.$message.success('状态切换成功')
      this.getDirectoryList()
    },
    // 修改
    edit (row) {
      this.showDirectorys = true
      this.$refs.DirectorysRef.chuancan(row)
    },
    // 删除
    remove (row) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: row.row.id })
        this.getDirectoryList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  .zz {
    font-weight: 700;
    margin-right: 10px;
  }
  .btn {
    margin-left: 10px;
  }
  .alert {
    margin: 10px 0;
  }
  .pagination {
    text-align: center;
    margin-top: 10px;
  }
  ::v-deep .sub {
    .el-breadcrumb__inner  {
      color: #606266 !important;
    }
  }
}
</style>
